//
//  ZYThreeGoodParentsController.m
//  优学荟管理系统
//
//  Created by edz on 2017/12/13.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "ZYThreeGoodParentsController.h"
#import "ZYThreeGoodParentsCell.h"
#import "ZYThreeGoodParentsModel.h"
#import "HNDistributionStudentVC.h"
#import "HMAddPatriarchVC.h"
#define kCell @"cell"
@interface ZYThreeGoodParentsController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) BOOL isTrue;
@end

@implementation ZYThreeGoodParentsController






- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self.navigationController.navigationBar setBackgroundImage:[LLTools createImageWithColor:ColorRGB(255, 255, 255)] forBarMetrics:(UIBarMetricsDefault)];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:ColorRGB(51,51,51)}];
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:ColorRGB(255,255,255)}];
    [self.navigationController.navigationBar setBackgroundImage:[LLTools createImageWithColor:ColorRGB(47, 204, 138)] forBarMetrics:(UIBarMetricsDefault)];

    [SVProgressHUD dismiss];
}









- (NSMutableArray *)dataSource{
    
    
    if (_dataSource == nil) {
        self.dataSource = [NSMutableArray array];
    }
    
    
    return _dataSource;
    
    
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    

    self.title = @"三好家长";
    
    [self setUpSuperView];
    [self setUpRefresh];
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTargat:self action:@selector(itemAction) image:@"icon_back_student_sign" highImage:@"icon_back_student_sign"];
    
   
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTargat:self action:@selector(rightItemAction) image:@"icon_add" highImage:@"icon_add"];

    
    
    
    
    
}


- (void)itemAction{
    
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


- (void)rightItemAction{
    //添加家长
    HMAddPatriarchVC *vc = [HMAddPatriarchVC new];
    vc.lessionId = self.lessionId;
    vc.isUpTwo = YES;
    __block typeof(self) weakSelf = self;
    vc.addSuccessBlock = ^{
        [weakSelf.tableView.mj_header beginRefreshing];
    };
    [self.navigationController pushViewController:vc animated:YES];
    
    
}




#pragma mark --- 上拉加载和下拉刷新方法的实现
- (void)setUpRefresh{
    
    //下拉刷新
    self.tableView.mj_header  = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(downRefresh)];
    //自动更改透明度
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    //进入刷新状态
    [self.tableView.mj_header beginRefreshing];
    
    //上拉加载
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(upRefresh)];
    //结束头部刷新
    [self.tableView.mj_header endRefreshing];
    //结束尾部刷新
    [self.tableView.mj_footer endRefreshing];
    
    
}

/**
 *  上拉加载
 */
- (void)upRefresh{
    
    self.isTrue = NO;
    self.page++;
    [self setNetWork];
    
}
/**
 *  下拉刷新
 */

- (void)downRefresh{
    self.isTrue = YES;
    self.page = 1;
    [self setNetWork];
    
    
}













- (void)setUpSuperView{
    
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64)];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.estimatedRowHeight = 200;
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.contentInset = UIEdgeInsetsMake(-54, 0, 0, 0);
    [self.tableView registerClass:[ZYThreeGoodParentsCell class] forCellReuseIdentifier:kCell];
    



    UIView *allView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight *30/1334)];
    allView.backgroundColor = ColorRGB(245, 245, 245);
    
    self.tableView.tableHeaderView = allView;
    
}





- (void)setNetWork{
    
    
    
    NSString *url = [NSString stringWithFormat:@"trainLesson.do?action=getParentList&oid=%@&pageNumber=%ld&lessonId=%@",[AccountTool shareAccountTool].currentAccount.oid,(long)self.page,self.lessionId];
    
    
    NSLog(@"%@  %@",LLBaseURL(url),[AccountTool shareAccountTool].currentAccount.authCode);
    
    [SVProgressHUD setDefaultStyle:(SVProgressHUDStyleCustom)];
    [SVProgressHUD  setBackgroundColor:ColorRGB(4, 4, 4)];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    [SVProgressHUD showWithStatus:LLLoading];   
    [NetManager Get:LLBaseURL(url) Auth:YES Params:nil Success:^(id Obj) {
        
        [SVProgressHUD dismiss];
        if ([[Obj valueForKey:@"code"] integerValue] == 1000) {
            
            if (self.isTrue == YES) {
                [self.dataSource removeAllObjects];
            }
            NSDictionary *t = [Obj valueForKey:@"t"];
            
            NSArray *arr = [t valueForKey:@"dataList"];
            
            
            for (NSDictionary *dict in arr) {
                ZYThreeGoodParentsModel *modle = [ZYThreeGoodParentsModel new];
                
                [modle setValuesForKeysWithDictionary:dict];
                
                
                [self.dataSource addObject:modle];
                
                
                
                
                
            }
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            
            
        }
        
        
        
    } Fail:^(NSError *Error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [SVProgressHUD dismiss];
    }];
    
    
    
    
    
    
    
    
}














































#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZYThreeGoodParentsCell *cell = [tableView dequeueReusableCellWithIdentifier:kCell forIndexPath:indexPath];
    
    
    if (self.dataSource.count > indexPath.row) {
        
        ZYThreeGoodParentsModel *model = self.dataSource[indexPath.row];
        
        
        [cell assignByZYThreeGoodParentsModel:model];
        
        
        
    }
    
    
    
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    
    [cell.staudentListButton addTarget:self action:@selector(staudentListButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [cell.deleteButton addTarget:self action:@selector(deleteButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    
    return cell;
}



#pragma mark --- 学生列表方法的实现
- (void)staudentListButtonAction:(UIButton *)sender{
    
    ZYThreeGoodParentsCell *cell = (ZYThreeGoodParentsCell *)sender.superview.superview;
    
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    ZYThreeGoodParentsModel *model = self.dataSource[indexPath.row];
    
//    model.uid   这个字段就是家长id
//   self.lessionId 这个字段就是课程id
    HNDistributionStudentVC *VC = [[HNDistributionStudentVC alloc] init];
    VC.Uid = @([model.uid integerValue]);
    VC.LessonID = @([self.lessionId integerValue]);

    [self.navigationController pushViewController:VC animated:YES];
    
    
    
}






#pragma mark ---- 删除方法的实现
- (void)deleteButtonAction:(UIButton *)sender{
    
    ZYThreeGoodParentsCell *cell = (ZYThreeGoodParentsCell *)sender.superview.superview;
    
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    ZYThreeGoodParentsModel *model = self.dataSource[indexPath.row];
    
    
    
    
    ZYalertController *alert = [ZYalertController alertControllerWithTitle:@"温馨提示" message:@"是否删除?" preferredStyle:(UIAlertControllerStyleAlert)];
    ZYAlertAction *action = [ZYAlertAction actionWithTitle:@"YES" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
        if (_dataSource.count > 1) {
            
            
            NSMutableArray *temp = [NSMutableArray arrayWithArray:_dataSource];
            
            [temp removeObjectAtIndex:indexPath.row];
            
            _dataSource = temp;
            
            NSArray *deleteItems = @[indexPath];
            
            [self.tableView deleteRowsAtIndexPaths:deleteItems withRowAnimation:(UITableViewRowAnimationRight)];
            
        }else{
            [_dataSource removeAllObjects];
            [self.tableView reloadData];
        }
        
        
        [self netMoonWorkId:[NSString stringWithFormat:@"%@",model.xtgpId]];
        
        
    }];
    
    ZYAlertAction *actionn = [ZYAlertAction actionWithTitle:@"NO" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    action.textColor = [UIColor blackColor];
    actionn.textColor = [UIColor blackColor];
    [alert addAction:action];
    [alert addAction:actionn];
    [self presentViewController:alert animated:YES completion:nil];
    
    
    
}



- (void)netMoonWorkId:(NSString *)ID{
    
//    http://192.168.0.226:8080/StarShine/ys/trainLesson.do?action=delParentById&id=1
    
    NSString *url = [NSString stringWithFormat:@"trainLesson.do?action=delParentById&id=%@",ID];
    
    [NetManager Get:LLBaseURL(url) Auth:YES Params:nil Success:^(id Obj) {
        
        
        
    } Fail:^(NSError *Error) {
        
    }];
    
    
}














@end
